﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Text;
using Maticsoft.Common;

namespace Lawyer.Web
{
	public partial class activitymodify : System.Web.UI.Page
	{
        Lawyer.BLL.ArchiveInfo archiveBll = new Lawyer.BLL.ArchiveInfo();
        Lawyer.BLL.ActivityTypeInfo bll = new Lawyer.BLL.ActivityTypeInfo();
        Lawyer.BLL.UserInfo userBll = new Lawyer.BLL.UserInfo();
        public string ActivityID = "";
		protected void Page_Load(object sender, EventArgs e)
		{
            if (!Page.IsPostBack)
            {
                ActivityTypeDataBind();
                ArchiveDataBind();
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    ActivityID = Request.Params["id"];
                    ShowInfo(ActivityID);
                }
            }
		}
        
        private void ShowInfo(string ActivityID)
        {
            Lawyer.Model.ActivityInfo model = new Lawyer.Model.ActivityInfo();
            Lawyer.BLL.ActivityInfo bll = new Lawyer.BLL.ActivityInfo();
            model = bll.GetModel(ActivityID);
            //this.txtActivityID.Value = model.ActivityID;

            this.ActivityTypeList.SelectedValue = model.Type;
            this.txtActivityName.Value = model.Title;
            this.txtActivityDetails.Text = model.Detail;
            this.txtActivityDetails.Text = model.Detail;
            this.txtStartTime.Value = model.StartDate.ToString();
            this.txtEndTime.Value = model.EndDate.ToString();
            this.txtFee.Value = model.Fee.ToString();
            //if (model.ArchiveID == null)
            //{
            //    this.archiveDiv.Visible = false;
            //    this.checkArchive.Checked = false;
            //}
            //else
            //{
            //    this.checkArchive.Checked = true;
            //    Model.ArchiveInfo archive = archiveBll.GetModel(model.ActivityID);
            //    if (archive != null)
            //    {
            //        this.ArchiveList.SelectedValue = archive.Title;
            //    }
            //}
        }

        

        public void ActivityTypeDataBind()
        {
            List<Model.ActivityTypeInfo> activityTypeMap = new List<Lawyer.Model.ActivityTypeInfo>();
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            ds = bll.GetList("");
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Model.ActivityTypeInfo typeinfo = new Lawyer.Model.ActivityTypeInfo();
                    typeinfo.TypeID = dt.Rows[i][0].ToString();
                    typeinfo.TypeName = dt.Rows[i][1].ToString();
                    activityTypeMap.Add(typeinfo);
                }
            }
            ActivityTypeList.DataSource = activityTypeMap;
            ActivityTypeList.DataTextField = "TypeName";
            ActivityTypeList.DataValueField = "TypeID";
            ActivityTypeList.DataBind();
        }

        public void ArchiveDataBind()
        {
            List<Model.ArchiveInfo> archiveList = new List<Lawyer.Model.ArchiveInfo>();
            Model.UserInfo userinfo = userBll.GetModel(Session["UserName"].ToString());
            string strWhere = "LawyerID = '" + userinfo.UserID + "'";
            DataSet ds = archiveBll.GetList(strWhere);
            DataTable dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Model.ArchiveInfo archive = new Lawyer.Model.ArchiveInfo();
                archive.ArchiveID = dt.Rows[i][0].ToString();
                archive.Title = dt.Rows[i][1].ToString();
                archiveList.Add(archive);
            }

            ArchiveList.DataSource = archiveList;
            ArchiveList.DataTextField = "Title";
            ArchiveList.DataValueField = "ArchiveID";
            ArchiveList.DataBind();
        }

        public string GetLawyerID(string LawyerName)
        {
            Model.UserInfo user = userBll.GetModel(LawyerName);
            return user.UserID;
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            Lawyer.Model.ActivityInfo model = new Lawyer.Model.ActivityInfo();
            Lawyer.BLL.ActivityInfo bll = new Lawyer.BLL.ActivityInfo();
            model = bll.GetModel(ActivityID);

            string strErr = "";
            if (this.txtActivityName.Value.Trim().Length == 0)
            {
                strErr += "Activity name can't be empty!";
            }
            if (this.txtActivityDetails.Text.Trim().Length == 0)
            {
                strErr += "Activity details can't be empty!";
            }
            if (!PageValidate.IsDecimal(txtFee.Value.ToString()))
            {
                strErr += "The activity fee format is not right!";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }

            string TypeID = this.ActivityTypeList.Items[this.ActivityTypeList.SelectedIndex].Text.ToString();
            string Title = this.txtActivityName.Value.ToString();
            string Detail = this.txtActivityDetails.Text.ToString();

            DateTime StartDate = DateTime.Parse(this.txtStartTime.Value.ToString());
            DateTime EndDate = DateTime.Parse(this.txtEndTime.Value.ToString());
            decimal Fee = decimal.Parse(this.txtFee.Value.ToString());
            string ArchiveID = "";
            int IsArchive = 0;
            if (checkArchive.Checked && ArchiveList.Items[ArchiveList.SelectedIndex].Text != "")
            {
                IsArchive = 1;
                ArchiveID = ArchiveList.SelectedValue.ToString();
            }
            
            //Status has three type, 1 is has no approve, 2 is approve but not do,3 is do.
            DateTime CreatedDate = System.DateTime.Now;

            Lawyer.Model.ActivityInfo UpdateModel = new Lawyer.Model.ActivityInfo();

            UpdateModel.ActivityID = model.ActivityID;
            UpdateModel.Type = TypeID;
            UpdateModel.Title = Title;
            UpdateModel.Detail = Detail;
            UpdateModel.LawyerID = GetLawyerID(Session["UserName"].ToString());
            UpdateModel.StartDate = StartDate;
            UpdateModel.EndDate = EndDate;
            UpdateModel.Fee = Fee;
            UpdateModel.ArchiveID = ArchiveID;
            UpdateModel.IsArchive = IsArchive;
            UpdateModel.Status = model.Status;
            UpdateModel.CreatedBy = model.CreatedBy;
            UpdateModel.CreatedDate = model.CreatedDate;

            if (bll.Update(UpdateModel))
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "Save successfully！", "activity.aspx");
            }
            else
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "Update failed！", "activity.aspx");
            }
            
        }
	}
}
